<!--
  * 暂收送检单行
  *
  * @Author:    AKITA
  * @Date:      2025-08-03 22:54:42
  * @Copyright  AKITA
-->
<template>


  <a-card :bordered="false">









    <!---------- 查询表单form begin ----------->
    <a-form class="smart-query-form">
      <a-row class="smart-query-form-row">
        <a-form-item label="显示零" class="smart-query-form-item">
          <a-checkbox v-model:checked="queryForm.zero"></a-checkbox>
        </a-form-item>
        <a-form-item label="物料编码" class="smart-query-form-item">
          <a-input allow-clear style="width: 200px" v-model:value="queryForm.itemId" placeholder="物料编码" />
        </a-form-item>
        <a-form-item label="物料名称" class="smart-query-form-item">
          <a-input allow-clear style="width: 200px" v-model:value="queryForm.itemName" placeholder="物料名称" />
        </a-form-item>
        <a-form-item label="系统批次" class="smart-query-form-item">
          <a-input allow-clear style="width: 200px" v-model:value="queryForm.batchId" placeholder="系统批次" />
        </a-form-item>

        <a-form-item class="smart-query-form-item">
          <a-button-group>
            <a-button type="primary" @click="onSearch">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery" class="smart-margin-left10">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-form-item>
      </a-row>
    </a-form>
    <!---------- 查询表单form end ----------->




    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block"></div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="null" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table :dataSource="tableData" :columns="columns" rowKey="id" :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ text, record, column }">
        <template v-if="column.dataIndex === 'quality'">
          <span>
            <span v-if="text === 'A'">合格</span>
            <span v-if="text === 'B'">让步接收</span>
            <span v-if="text === 'C'">不合格</span>
          </span>
        </template>

        <template v-if="column.dataIndex === 'qty'">
          <span>{{ excute(text) }}</span>
        </template>

      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize" v-model:current="queryForm.pageNum" v-model:pageSize="queryForm.pageSize"
        :total="total" @change="queryData" @showSizeChange="queryData" :show-total="(total) => `共${total}条`" />
    </div>
  </a-card>
</template>
<script setup>
import { reactive, ref, onMounted } from 'vue';
import { message, Modal } from 'ant-design-vue';
import { SmartLoading } from '/src/components/framework/smart-loading';
import { inventordApi } from '/src/api/business/erp/inventord-api';
import { PAGE_SIZE_OPTIONS } from '/src/constants/common-const';
import { smartSentry } from '/src/lib/smart-sentry';
import TableOperator from '/src/components/support/table-operator/index.vue';

import { excute } from '/@/utils/numformat-util.js';

// ---------------------------- 表格列 ----------------------------

const columns = ref([
  {
    title: '物料编码',
    dataIndex: 'itemId',
    align: 'center',
    ellipsis: true,
    width: 260,
  },
  {
    title: '物料名称',
    dataIndex: 'itemName',
    align: 'center',
    ellipsis: true,
  },
  {
    title: '规格',
    dataIndex: 'specs',
    align: 'left',
  },
  {
    title: '单位',
    dataIndex: 'unit',
    align: 'center',
  },
  {
    title: '数量',
    dataIndex: 'qty',
    align: 'right',
  },

  {
    title: '系统批次',
    dataIndex: 'batchId',
    align: 'center',
  },
  {
    title: '质量',
    dataIndex: 'quality',
    align: 'center',
  },
  {
    title: '仓库编码',
    dataIndex: 'stoId',
    align: 'center',
  },
  {
    title: '仓库名称',
    dataIndex: 'stoName',
    align: 'center',
  },
]);

// ---------------------------- 查询数据表单和方法 ----------------------------

const queryFormState = {
  pageNum: 1,
  pageSize: 40,
  itemId: undefined,
  zero: false,
};
// 查询表单form
const queryForm = reactive({ ...queryFormState });
// 表格加载loading
const tableLoading = ref(false);
// 表格数据
const tableData = ref([]);
// 总数
const total = ref(0);

// 重置查询条件
function resetQuery() {
  let pageSize = queryForm.pageSize;
  Object.assign(queryForm, queryFormState);
  queryForm.pageSize = pageSize;
  queryData();
}

// 搜索
function onSearch() {
  queryForm.pageNum = 1;
  queryData();
}

// 查询数据
async function queryData() {
  tableLoading.value = true;
  try {
    let queryResult = await inventordApi.queryPage(queryForm);
    tableData.value = queryResult.data.list;
    total.value = queryResult.data.total;
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    tableLoading.value = false;
  }
}

onMounted(queryData);

// ---------------------------- 添加/修改 ----------------------------
const formRef = ref();

function showForm(data) {
  formRef.value.show(data);
}

// ---------------------------- 单个删除 ----------------------------
//确认删除
function onDelete(data) {
  Modal.confirm({
    title: '提示',
    content: '确定要删除选吗?',
    okText: '删除',
    okType: 'danger',
    onOk() {
      requestDelete(data);
    },
    cancelText: '取消',
    onCancel() { },
  });
}

//请求删除
async function requestDelete(data) {
  SmartLoading.show();
  try {
    let deleteForm = {
      goodsIdList: selectedRowKeyList.value,
    };
    await inventordApi.delete(data.id);
    message.success('删除成功');
    queryData();
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    SmartLoading.hide();
  }
}

// ---------------------------- 批量删除 ----------------------------

// 选择表格行
const selectedRowKeyList = ref([]);

function onSelectChange(selectedRowKeys) {
  selectedRowKeyList.value = selectedRowKeys;
}

// 批量删除
function confirmBatchDelete() {
  Modal.confirm({
    title: '提示',
    content: '确定要批量删除这些数据吗?',
    okText: '删除',
    okType: 'danger',
    onOk() {
      requestBatchDelete();
    },
    cancelText: '取消',
    onCancel() { },
  });
}

//请求批量删除
async function requestBatchDelete() {
  try {
    SmartLoading.show();
    await inventordApi.batchDelete(selectedRowKeyList.value);
    message.success('删除成功');
    queryData();
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    SmartLoading.hide();
  }
}
</script>
